package dtm;

import tm.TMConfiguration;

import java.util.Set;

/**
 * @author haoly
 * @title DTM
 * @date 2025/4/23 14:45
 * @description 确定性图灵机类
 *
 *
 */
public class DTM <T>{
    private TMConfiguration<T> currentConfiguration;

    private Set<T> acceptStates;

    private DTMRulebook<T> rulebook;

    public DTM(TMConfiguration<T> currentConfiguration, Set<T> acceptStates, DTMRulebook<T> rulebook) {
        this.currentConfiguration = currentConfiguration;
        this.acceptStates = acceptStates;
        this.rulebook = rulebook;
    }

    public Boolean accepting(){
        T state = currentConfiguration.getState();
        return acceptStates.contains(state);
    }

    public void run(){
        while (!accepting() && !stuck()) {
            step();
        }
    }

    public void step() {
        TMConfiguration<T> nextConfiguration = rulebook.nextConfiguration(currentConfiguration);
        this.currentConfiguration = nextConfiguration;
    }

    public TMConfiguration<T> getCurrentConfiguration() {
        return currentConfiguration;
    }

    public Set<T> getAcceptStates() {
        return acceptStates;
    }

    public DTMRulebook<T> getRulebook() {
        return rulebook;
    }

    public Boolean stuck(){
        return !accepting() && !rulebook.appliesTo(currentConfiguration);
    }
}
